/*
    Meter JMX Utility Library
    Copyright (C) 2005  Noah Campbell

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
package info.noahcampbell.meter;


import java.util.HashMap;
import java.util.Map;

import javax.management.ObjectName;

/**
 * @author Noah Campbell
 * @version 1.0
 */
public final class MetricFactory {

    /**
     * The meters. 
     */
    private static Map<ObjectName, Meter> meters = new HashMap<ObjectName, Meter>();
    
    private static Map<ObjectName, Counter> counters = new HashMap<ObjectName, Counter>();
    /**
     * Construct a new <code>MetricFactory</code>.
     *
     */
    private MetricFactory() {
    }

    /**
     * @param name 
     * @return meter
     * @throws NullPointerException
     */
    public static synchronized Meter getMeter(String name) {
        
        ObjectName oname;
        try {
            oname = new ObjectName(name);
            return getMeter(oname);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
    }

    /**
     * @param name
     * @return meter
     */
    public static synchronized Meter getMeter(ObjectName name) {
        Meter meter = meters.get(name);
        if(meter == null) {
            meter = new Meter();
            meters.put(name, meter);
        }
        return meter;
    }
    
    
    
    /**
     * @param name
     * @return
     */
    public static Counter getCounter(String name) {
        ObjectName oname;
        try {
            oname = new ObjectName(name);
            return getCounter(oname);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public synchronized static Counter getCounter(ObjectName oname) {
        Counter c = counters.get(oname);
        if(c == null) {
            c = new Counter();
            counters.put(oname, c);
        }
        return c;
    }
    

}
